// Package controllers
// @Title  uploadImage.go
// @Description  图片上传控制器
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/models"
	"beego-admin/service"
	"github.com/astaxie/beego"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

type UploadImageController struct {
	BaseController
}

type MyPutRet struct {
	Key    string
	Hash   string
	Fsize  int
	Bucket string
	Name   string
}

func (this *UploadImageController) Index() {
	this.TplName = "admin/image_manage/image_list.tpl"
}

func (this *UploadImageController) UploadImage() {
	this.TplName = "admin/image_manage/upload_image.tpl"
}

func (this *UploadImageController) GetImageList(){
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	list, count, _ := models.GetImageList(pageSize,page)
	type imageObj struct{
		Id                 int    `json:"id"`
		Title               string `json:"title"`
		ImageUrl               string `json:"image_url"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]imageObj
	imageHost := beego.AppConfig.String("qiniu::imageHost")
	for _, v := range *list {
		tmpParam := imageObj{v.Id,v.Title, imageHost+v.ImageUrl,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}


func (this *UploadImageController) AjaxUploadImage() {
	f, h, err := this.GetFile("file")

	if err != nil {
		this.Data["json"] = this.AjaxReturn(10001,"上传失败",nil)
		this.ServeJSON()
		this.StopRun()
	}

	defer f.Close()
	file, _ := h.Open() // 这里获得的实际就是一个io,通过源码看到这个open方法最终返回的是一个结构体,其内部包含了 io.Reader的接口
	rand.Seed(time.Now().Unix())  //Seed生成的随机数
	filename := strconv.FormatInt(time.Now().Unix(),10) + strconv.FormatInt(rand.Int63n(10000),10)
	if filename, err = service.Upload(file, h.Size, filename); err != nil { // 通过h.size 即可获得文件大小
		this.Data["json"] = this.AjaxReturn(10002,"上传失败",nil)
	} else {
		this.Data["json"] = this.AjaxReturn(10000,"上传成功",filename)
	}

	this.ServeJSON()
}

func (this *UploadImageController) AjaxAddImage() {
	title := this.GetString("title")
	imageUrl := this.GetString("image_url")
	if title == "" || imageUrl == ""{
		this.Data["json"] = this.AjaxReturn(10001,"标题和图片不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.AddImage(title,imageUrl)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

// AjaxImageDelete ajax删除图片
func (this *UploadImageController) AjaxImageDelete() {
	imageId,_ := this.GetInt("id",0)

	adminUserInfo,_ := models.GetImageInfoById(imageId)
	if adminUserInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteImageById(imageId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}

// AjaxImageDeleteMore ajax批量删除图片
func (this *UploadImageController) AjaxImageDeleteMore() {
	imageIds := this.GetString("ids")
	imageIds = imageIds[0:len(imageIds)-1]
	imageArr := strings.Split(imageIds,",")

	id := models.DeleteMoreImageById(imageArr)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}